package com.kzd.gtgame.main.web

import android.view.View
import android.webkit.CookieManager
import android.webkit.WebView
import com.kzd.gtgame.R
import com.kzd.gtgame.callback.AppWebViewClient
import com.kzd.gtgame.custom.createViewWeb
import com.kzd.gtgame.main.KEY_RICH_TEXT_TITLE
import com.module.lemlin.owner.OwnerViewModeActivity
import com.module.lemlin.owner.OwnerViewModel
import kotlinx.android.synthetic.main.activity_web_view.*

abstract class WebViewActivity<ViewModel : OwnerViewModel> : OwnerViewModeActivity<ViewModel>() {


    private val mTitle: String?
        get() = intent?.extras?.getString(KEY_RICH_TEXT_TITLE)

    val mWebView: WebView by lazy {
        createViewWeb(context = this).apply { webViewClient = AppWebViewClient() }
    }

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flWebViewBar)
    }

    override val layoutResId: Int
        get() = R.layout.activity_web_view

    override fun initView() {
        flWebViewBar.setTitleTextContent(mTitle)
        flWebViewBar.visibility = if (mTitle.isNullOrEmpty())
            View.GONE else View.VISIBLE
        mWebViewBody.addView(mWebView)
    }

    override fun onResume() {
        super.onResume()
        mWebView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mWebView.onPause()
    }

    override fun onDestroy() {
        if (mWebView.windowToken != null) {
            mWebViewBody.removeView(mWebView)
        }
        mWebView.clearHistory()
        mWebView.destroy()
        CookieManager.getInstance().removeAllCookies(null)
        CookieManager.getInstance().flush()
        super.onDestroy()
    }

    fun WebView.loadDataWithBaseURL(content: String) {
        loadDataWithBaseURL((null), content, ("text/html"), ("utf-8"), (null))
    }
}